package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import control.CControl;

public class PSafetyStatus extends JPanel implements Observer {
	
	private long safetystatus = 0x00;
	private String[] labels = {
			"Right Shutdown",
			"Left Shutdown",
			"Cockpit Shutdown",
			"Inertia Switch",
			"Brake Overtravel",
			"Motor Controller",
			"BMS General",
			"BMS Low Voltage",
			"IMD OK",
			"Brake Panic Fault OK",
			"V Safety LMN Out OK",
			"Tractive Testpoint IL",
			"Tractive Power IL",
			"Safety System OK",
			"Safety System Active OK"
			};
	
	public PSafetyStatus(CControl cControl)
	{
		
		
		
		this.setBackground(Color.white);
		
		cControl.addRegisterObserver("LPN_SafetyStatus", this);
		
	}
	
	protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	Graphics2D g2 = (Graphics2D)g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    			RenderingHints.VALUE_ANTIALIAS_ON);
    	
    	g2.setStroke(new BasicStroke(5));
    	//g2.translate(20, 200);
    	//g2.rotate(Math.toRadians(-90));
    	
    	drawCircuitoutline(g2,10,30);	
	}
	
	void drawCircuitoutline(Graphics2D g2,int offsetx,int offsety)
	{
		
    	// pack outline
    	GeneralPath p = new GeneralPath();
    	
    	g2.setStroke(new BasicStroke(6));
    	
    	p.moveTo(offsetx+15, offsety);
    	p.lineTo(offsetx+15, offsety+600);
    	
    	g2.draw(p);
    	
    	for (int i = 0; i < 15; i++) 
    	{
    		if((safetystatus & (1 << i)) > 0)
    		{
    			g2.setColor(Color.green);
    		}
    		else
    		{
    			g2.setColor(Color.red);
    		}
    		
    		g2.fillOval(offsetx, offsety + i*40, 30, 30);
    		g2.setColor(Color.black);
    		g2.drawString(labels[i], offsetx +30, offsety+ i*40 + 20);
    		
		}
    	
    	
    	
    	
	}
	
	@Override
	public void update(Observable o, Object arg) {
        //MY this.safetystatus = Integer.parseInt((String) arg, 2);
		this.safetystatus = (int) Long.parseLong((String) arg, 2);
		System.out.print(arg + "\n");
		this.updateUI();
	}

}
